package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门列表
     * REST:通过请求方式来表达请求行为..
     */
    @GetMapping
    public Result listDepts(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询部门列表数据", name, status, page, pageSize);
        PageResult<Dept> pageResult = deptService.listDepts(name, status, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 新增部门
     */
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        //@RequestBody 表示接收json数据
        log.info("查询部门列表数据", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    /**
     * 根据ID查询部门
     * @PathVariable:从路径变量中获取名字叫id的变量值赋给方法的形参
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询部门数据", id);
        Object dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("修改部门列表数据", dept);
        deptService.updateDept(dept);
        return Result.success();
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) {
        log.info("删除部门列表数据", id);
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询全部部门
     */
    @GetMapping("/list")
    public Result listAllDepts() {
        log.info("查询全部部门列表数据");
        List<Dept> list = deptService.listAllDepts();
        return Result.success(list);
    }
}